package com.gq.tab.scr;

import java.io.InputStream;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.ads.AdRequest;
import com.google.ads.AdView;
import com.gq.sherlockhomes.R;
import com.gq.sherlockhomes.ReaderActivity;
import com.rtst.widget.actionbar.ISearchInterface;
import com.rtst.widget.tab.TabFragment;

public class CategoryScr extends TabFragment implements ISearchInterface {
	
	private static final String TAG = "CategoryScr";
	private ViewGroup mLayout;
	private ListView listView;
	private StoryAdapter adapter;
	private int mType = TYPE_HOME;
	public static final int TYPE_HOME = 0x0;
	private AdView adView;
	
	public CategoryScr() {
		mType = TYPE_HOME;
	}
	
	public CategoryScr(int type) {
		mType = type;
	}
	
	public CategoryScr(String previousScrTag, int type) {
		super(previousScrTag);
		mType = type;
	}
	
	public CategoryScr(int type, String gd) {
		mType = type;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if(mLayout == null) {
			mLayout = (ViewGroup) inflater.inflate(R.layout.category_scr_layout, container, false);
			Log.i(TAG, getPreviousScrTag() + getTag() + mType);
			setUpForTypeHome();
		} else {
			((ViewGroup) mLayout.getParent()).removeView(mLayout);
		}
		adView = (AdView) mLayout.findViewById(R.id.adView);
		return mLayout;
	}
	
	@Override
	public void onResume() {
		super.onResume();
		adView.loadAd(new AdRequest());
	}

	private void setUpForTypeHome() {
		listView = (ListView) mLayout.findViewById(android.R.id.list);
		adapter = new StoryAdapter(getActivity());
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(getActivity(), ReaderActivity.class);
				intent.putExtra("POSITION", position);
				String prefix = "a";
				int intro = R.drawable.tap1;
				int pagesCount = 168;
				if (position == 0) {
					prefix = "a";
					pagesCount = 170;
					intro = R.drawable.tap1;
				} else if (position == 1) {
					prefix = "b";
					pagesCount = 313;
					intro = R.drawable.tap2;
				}
				intent.putExtra("PREFIX", prefix);
				intent.putExtra("PAGESCOUNT", pagesCount);
				intent.putExtra("INTRO", intro);
				getActivity().startActivity(intent);
			}
			
		});
		listView.setAdapter(adapter);
//		try {
//			adapter.load(getActivity().getAssets().open("data.xml"));
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
	}

	@Override
	public void onDestroy() {
		if (adView != null) adView.destroy();
		super.onDestroy();
	}

	@Override
	public void onSearchComplete(final CharSequence in) {
	}
	
	private class StoryAdapter extends BaseAdapter {
		
		private LayoutInflater mInflater;
		private String[] data = new String[] {"Sherlock Homes Phần 1", "Sherlock Homes Phần 2"};
		
		public StoryAdapter(Context c) {
			mInflater = LayoutInflater.from(c);
		}
		
		public void load(final InputStream is) {
//			new Thread(new Runnable() {
//				
//				@Override
//				public void run() {
//					data = new XMLParser(is).parse();
//					System.out.println("size of data:" + data.size());
//					listView.setAdapter(adapter);
//				}
//			}).start();
//			data = new XMLParser(is).parse();
//			listView.setAdapter(adapter);
		}
		
		@Override
		public int getCount() {
			return data.length;
		}

		@Override
		public Object getItem(int position) {
			return data[position];
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.item_list, null, false);
			}
			TextView textView = (TextView) convertView.findViewById(R.id.text_view);
			textView.setText(String.valueOf(getItem(position)));
			return convertView;
		}
	}
}
